package club.baimahu.pencil.domain.entity;

import club.baimahu.pencil.domain.entity.account.Account;
import club.baimahu.pencil.domain.entity.account.AccountManager;
import club.baimahu.pencil.domain.entity.account.CashAccount;
import club.baimahu.pencil.domain.entity.account.SimpleAccountManager;
import club.baimahu.pencil.domain.entity.location.ChinaLocationMap;
import club.baimahu.pencil.domain.entity.location.DefaultLocation;
import club.baimahu.pencil.domain.entity.tree.TreeNode;
import club.baimahu.pencil.domain.session.GoodsFactory;
import club.baimahu.pencil.domain.session.SimpleGoodFactory;
import club.baimahu.pencil.domain.session.Supermarket;
import lombok.Data;

import java.math.BigDecimal;

/**
 * 超市构建者,简化初始化步骤
 */
@Data
public class SuperMarketBuilder {
    private String province;
    private String city;
    private String region;
    private String detailAddress;

    Account cacheAccount;

    GoodsFactory goodsFactory;

    AccountManager accountManager;

/*    public SuperMarketBuilder stockWithGoods(List<GoodsStock> goodsStockList){
        if(goodsFactory == null){
            throw new BuyException("goodsFactory should init before stockWithGoods");
        }
        goodsFactory.stockWithGoods(goodsStockList);
        return this;
    }*/

    public SuperMarketBuilder province(String province){
        this.province = province;
        return this;
    }

    public SuperMarketBuilder city(String city){
        this.city = city;
        return this;
    }

    public SuperMarketBuilder region(String region){
        this.region = region;
        return this;
    }

    public SuperMarketBuilder detailAddress(String detailAddress){
        this.detailAddress = detailAddress;
        return this;
    }

    public SuperMarketBuilder goodsFactory(GoodsFactory goodsFactory){
        this.goodsFactory = goodsFactory;
        return this;
    }

    public SuperMarketBuilder cacheAccount(String accountName, BigDecimal money){
        this.cacheAccount = new CashAccount(accountName,money);
        return this;
    }

    public Supermarket build(String name){
        if(this.goodsFactory == null){
            goodsFactory = new SimpleGoodFactory();
        }
        Supermarket supermarket = new Supermarket();
        supermarket.setName(name);
        DefaultLocation defaultLocation = new DefaultLocation();

        ChinaLocationMap chinaLocationMap = ChinaLocationMap.getChinaLocationMap();
        TreeNode treeNode1 = chinaLocationMap.getNodeNameMap().get(this.province);
        TreeNode treeNode2 = chinaLocationMap.getNodeNameMap().get(this.city);
        TreeNode treeNode3 = chinaLocationMap.getNodeNameMap().get(this.region);
        defaultLocation.setNode1(treeNode1);
        defaultLocation.setNode2(treeNode2);
        defaultLocation.setNode3(treeNode3);
        defaultLocation.setDetailAddress(detailAddress);
        supermarket.setLocation(defaultLocation);

        supermarket.setAccountManager(this.accountManager == null ? new SimpleAccountManager(name,this.cacheAccount):this.accountManager);
        supermarket.setGoodsFactory(this.goodsFactory == null ? new SimpleGoodFactory(supermarket.getAccountManager()):this.goodsFactory);
        this.goodsFactory.setAccountManager(supermarket.getAccountManager());


        return supermarket;
    }

/*    public SuperMarketBuilder build(){
        return new SuperMarket();
    }*/


}
